//
//  BuyerSellerViewController.m
//  nonghubao
//
//  Created by 王 文彬 on 13-8-14.
//  Copyright (c) 2013年 Chongqing Nonghubao Co., Ltd. All rights reserved.
//

#import "BuyerSellerViewController.h"
#import "Config.h"
#import "Helper.h"
#import "RTLabel.h"
#import "JSONRequest.h"
#import "SellerServiceViewController.h"
#import "OrderListViewController.h"
#import "ProductListViewController.h"
#import "BuyerCommonListViewController.h"
#import "OrderManagementViewController.h"
#import "SellerServiceGuardViewController.h"
#import "LogisticsManagementViewController.h"

@interface BuyerSellerViewController ()

@end

@implementation BuyerSellerViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (id)initWithType:(BusinessType)type
{
    self = [super init];
    if (self) {
        _type = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.title = _type ? _L(@"vc_title_070") : _L(@"vc_title_071");
    self.view.backgroundColor = [UIColor whiteColor];
    
    //本视图应该是一个滚动视图
    UIScrollView* scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
    scrollView.backgroundColor = [UIColor whiteColor];
    scrollView.contentSize = CGSizeMake(_SYS_FRAME_WIDTH, 524);
    self.view = scrollView;

    //买家视图
    if (_type == 0) {

        [self.view addSubview:[self makeBuyerView]];        
    }
    //卖家视图
    else {
        
        [self.view addSubview:[self makeSellerView]];
        self.view.hidden = true;  //卖家视图默认隐藏，当卖家请求成功且是卖家时，才显示
    }
}

#pragma mark - 卖、买家视图载入机制 和 处理流程

- (void)viewDidAppear:(BOOL)animated
{
    JSONRequest* request = [[JSONRequest alloc] init];
    request.hudOn = true;
    request.hudTitle = _L(@"title_loading_01");
    request.delegate = (id)self;
    
    if (_type == 0) {
        
        request.URLString = _JSON_URL_PRODUCT_BUYER;
        request.successSelector = @selector(requestBuyerStatisticsSuccess:);
    }
    else {
        
        request.URLString = _JSON_URL_PRODUCT_SELLER;
        request.successSelector = @selector(requestSellerStatisticsSuccess:);
    }
    
    [request asynchronous];
}

// 买家状态请求成功
- (void)requestBuyerStatisticsSuccess:(NSDictionary *)dict
{
    if ([dict[@"status"] intValue] == 1) {
        
        NSInteger waitPayCount          = [dict[@"data"][@"waitPayCount"] integerValue];
        NSInteger waitDeliveryCount     = [dict[@"data"][@"waitDeliveryCount"] integerValue];
        NSInteger waitReceivingCount    = [dict[@"data"][@"waitReceivingCount"] integerValue];
        NSInteger commentReceivingCount = [dict[@"data"][@"commentReceivingCount"] integerValue];
        NSInteger refundReceivingCount  = [dict[@"data"][@"refundReceivingCount"] integerValue];
        NSInteger returnReceivingCount  = [dict[@"data"][@"returnReceivingCount"] integerValue];
        NSInteger historySuccessCount   = [dict[@"data"][@"historySuccessCount"] integerValue];
        NSInteger buyerProductCount     = [dict[@"data"][@"buyerProductCount"] integerValue];
        NSInteger collectProductCount   = [dict[@"data"][@"collectProductCount"] integerValue];
        
        UIView* view = self.view.subviews[0];
        
        NSString* statisticsString = [NSString stringWithFormat:_L(@"tpl_41"), waitPayCount, waitDeliveryCount, waitReceivingCount, commentReceivingCount, refundReceivingCount, returnReceivingCount, historySuccessCount];
        [((UIWebView *)view.subviews[0]) loadHTMLString:statisticsString baseURL:nil];
        
        ((RTLabel *)view.subviews[1]).text = [NSString stringWithFormat:_L(@"tpl_42"), buyerProductCount];
        ((RTLabel *)view.subviews[2]).text = [NSString stringWithFormat:_L(@"tpl_42"), collectProductCount];
    }
    else {
        
        [Helper showAlertWithTitle:dict[@"mesage"] andMessage:nil];
    }
}

// 卖家状态请求成功
- (void)requestSellerStatisticsSuccess:(NSDictionary *)dict
{
    if ([dict[@"status"] intValue] == 1) {
        
        NSInteger waitPayCount          = [dict[@"data"][@"waitPayCount"] integerValue];
        NSInteger waitDeliveryCount     = [dict[@"data"][@"waitDeliveryCount"] integerValue];
        NSInteger waitReceivingCount    = [dict[@"data"][@"waitReceivingCount"] integerValue];
        NSInteger commentReceivingCount = [dict[@"data"][@"commentReceivingCount"] integerValue];
        NSInteger refundReceivingCount  = [dict[@"data"][@"refundReceivingCount"] integerValue];
        NSInteger returnReceivingCount  = [dict[@"data"][@"returnReceivingCount"] integerValue];
        NSInteger historySuccessCount   = [dict[@"data"][@"historySuccessCount"] integerValue];
        NSInteger productCount          = [dict[@"data"][@"productCount"] integerValue];
        NSInteger productSellCount      = [dict[@"data"][@"productSellCount"] integerValue];
        NSInteger productStopSellCount  = [dict[@"data"][@"productStopSellCount"] integerValue];
        NSInteger sellerType            = [dict[@"data"][@"sellerType"] integerValue];
        NSString* sellerExpiredTime     = dict[@"data"][@"sellerExpiredTime"];

        _seller = dict[@"data"];
        if (sellerType == 0) {
            
            SellerServiceViewController* sellerServiceViewController = [[SellerServiceViewController alloc] initWithSeller:_seller];
            [self.navigationController pushViewController:sellerServiceViewController animated:false];
            return;
        }
        
        //显示卖家视图
        self.view.hidden = false;
        
        UIView* view = self.view.subviews[0];
        
        NSString* statisticsString = [NSString stringWithFormat:_L(@"tpl_41"), waitPayCount, waitDeliveryCount, waitReceivingCount, commentReceivingCount, refundReceivingCount, returnReceivingCount, historySuccessCount];
        [((UIWebView *)view.subviews[0]) loadHTMLString:statisticsString baseURL:nil];
        
        NSString* productsString = [NSString stringWithFormat:_L(@"tpl_43"), productCount, productSellCount, productStopSellCount];
        [((UIWebView *)view.subviews[1]) loadHTMLString:productsString baseURL:nil];
        
        NSString* sellerStateString;
        if (sellerType == 1) {
            
            sellerStateString = _L(@"tpl_44");
        }
        else if (sellerType == 2 || sellerType == 3) {
            
            sellerStateString = _L(@"tpl_45");
        }
        ((RTLabel *)view.subviews[2]).text = sellerStateString;
        ((RTLabel *)view.subviews[3]).text = [NSString stringWithFormat:_L(@"tpl_47"), sellerExpiredTime];
    }
    else {
        
        [Helper showAlertWithTitle:dict[@"message"] andMessage:nil];
    }
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 产生买家视图 和 点击事件
/*!
 @method
 @abstrsct 产生一个买家视图
 */
- (UIView *)makeBuyerView
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, 460)];
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"transaction_img_011"]];
    
    UIWebView* webView = [[UIWebView alloc] initWithFrame:CGRectMake(3, 55, _SYS_FRAME_WIDTH - 20, 150)];
    [webView loadHTMLString:[NSString stringWithFormat:_L(@"tpl_41"), 0, 0, 0, 0, 0, 0, 0] baseURL:nil];
    webView.scrollView.scrollEnabled = false;
    [view addSubview:webView];
    
    RTLabel* label1 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 290, _SYS_FRAME_WIDTH - 20, 40)];
    label1.text = [NSString stringWithFormat:_L(@"tpl_42"), 0];
    [view addSubview:label1];
    
    RTLabel* label2 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 410, _SYS_FRAME_WIDTH - 20, 40)];
    label2.text = [NSString stringWithFormat:_L(@"tpl_42"), 0];
    [view addSubview:label2];
    
    //buttons
    UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, 200)];
    [button1 addTarget:self action:@selector(orderButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button1];
    
    UIButton* button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 210, _SYS_FRAME_WIDTH, 110)];
    [button2 addTarget:self action:@selector(boughtButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button2];
    
    UIButton* button3 = [[UIButton alloc] initWithFrame:CGRectMake(0, 330, _SYS_FRAME_WIDTH, 110)];
    [button3 addTarget:self action:@selector(collectButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button3];
    
    return view;
}


#pragma mark - 产生卖家视图 和 点击事件
/*!
 @method
 @abstract 产生卖家视图
 */
- (UIView *)makeSellerView
{
    UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, 524)];
    view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"transaction_img_012"]];
    
    UIWebView* webView1 = [[UIWebView alloc] initWithFrame:CGRectMake(3, 55, _SYS_FRAME_WIDTH - 20, 150)];
    [webView1 loadHTMLString:[NSString stringWithFormat:_L(@"tpl_41"), 0, 0, 0, 0, 0, 0, 0] baseURL:nil];
    webView1.scrollView.scrollEnabled = false;
    [view addSubview:webView1];
    
    UIWebView* webView2 = [[UIWebView alloc] initWithFrame:CGRectMake(3, 285, _SYS_FRAME_WIDTH - 20, 65)];
    [webView2 loadHTMLString:[NSString stringWithFormat:_L(@"tpl_43"), 0, 0, 0] baseURL:nil];
    webView2.scrollView.scrollEnabled = false;
    [view addSubview:webView2];
    
    RTLabel* label1 = [[RTLabel alloc] initWithFrame:CGRectMake(10, 500, _SYS_FRAME_WIDTH - 20, 40)];
    label1.text = [NSString stringWithFormat:_L(@"tpl_42"), 0];
    [view addSubview:label1];
    
    RTLabel* label2 = [[RTLabel alloc] initWithFrame:CGRectMake(150, 502.5, 150, 40)];
    label2.font = _SYS_FONT_001;
    label2.textAlignment = RTTextAlignmentRight;
    label2.textColor = [UIColor grayColor];
    [view addSubview:label2];
    
    //buttons
    UIButton* button1 = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, _SYS_FRAME_WIDTH, 210)];
    [button1 addTarget:self action:@selector(orderButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button1];
    
    UIButton* button2 = [[UIButton alloc] initWithFrame:CGRectMake(0, 220, _SYS_FRAME_WIDTH, 130)];
    [button2 addTarget:self action:@selector(productButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button2];
    
    UIButton* button3 = [[UIButton alloc] initWithFrame:CGRectMake(0, 360, _SYS_FRAME_WIDTH, 60)];
    [button3 addTarget:self action:@selector(logisticsButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button3];
    
    UIButton* button4 = [[UIButton alloc] initWithFrame:CGRectMake(0, 425, _SYS_FRAME_WIDTH, 90)];
    [button4 addTarget:self action:@selector(sellerServiceButtonEvent) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button4];

    return view;
}

#pragma mark - 卖、买家视图的按钮点击事件
- (void)orderButtonEvent
{
    OrderManagementViewController* orderManagementViewController = [[OrderManagementViewController alloc] initWithBusinessType:_type];
    orderManagementViewController.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:orderManagementViewController animated:true];
}

- (void)boughtButtonEvent
{
    BuyerCommonListViewController *buyerCommonListVC = [[BuyerCommonListViewController alloc] initWithStyle:UITableViewStylePlain andCategory:BuyerServiceCategoryBought];
    buyerCommonListVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:buyerCommonListVC animated:YES];
}

- (void)collectButtonEvent
{
    BuyerCommonListViewController *buyerCommonListVC = [[BuyerCommonListViewController alloc] initWithStyle:UITableViewStylePlain andCategory:BuyerServiceCategoryFavorited];
    buyerCommonListVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:buyerCommonListVC animated:YES];
}

- (void)productButtonEvent
{
    ProductListViewController* productListViewController = [[ProductListViewController alloc] init];
    productListViewController.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:productListViewController animated:true];
}

- (void)sellerServiceButtonEvent
{
    SellerServiceGuardViewController* sellerServiceGuardViewController = [[SellerServiceGuardViewController alloc] initWithSeller:_seller];
    sellerServiceGuardViewController.hidesBottomBarWhenPushed = true;
    [self.navigationController pushViewController:sellerServiceGuardViewController animated:true];
}

- (void)logisticsButtonEvent
{
    LogisticsManagementViewController *logisticsManagementVC = [[LogisticsManagementViewController alloc] init];
    logisticsManagementVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:logisticsManagementVC animated:YES];
}

#pragma mark - JSONRequest Delegate

- (void)requestFailWithNetworkUnavailabe
{
    [Helper showAlertWithTitle:_L(@"title_networkunavailabe") andMessage:nil];
}

@end
